/*------------------------------

	Space Is The Place v 0.1
		by Sestze

------------------------------*/

-- reflected-sounds-of-underground-spirits
-- they are called creds. fuck it.

CreateConVar( "sitp_defaultcredits", 20000 )

SITP_FILEPATH = "sitp/economy.txt"

SITP_NextEconTick = 0

function SITP_GetCreds ( ply )
	local id = ply:SteamID()
	local defcreds = tostring(GetConVarNumber( "sitp_defaultcredits" ))
	local creds = 0
	
	if(file.Exists( SITP_FILEPATH )) then
		local content = file.Read( SITP_FILEPATH )
		local findid = string.find( content, id )
		print( "findid has a value of " .. tostring(findid))
		if ( findid == nil ) then
			content = id .. " " .. defcreds .. "\n"
			file.Append( SITP_FILEPATH, content )
			creds = GetConVarNumber( "sitp_defaultcredits" )
		else
			local contenttable = string.Explode( "\n", content )
			for k, v in pairs (contenttable) do
				if ( string.find( v, id ) ) then
					local smalltbl = string.Explode( " ", v )
					creds = tonumber(smalltbl[2])
				end
			end
		end
	else
		local content = id .. " " .. defcreds .. "\n"
		file.Write( SITP_FILEPATH, content )
		creds = GetConVarNumber( "sitp_defaultcredits" )
	end
	
	local mincreds = GetConVarNumber( "sitp_defaultcredits" ) 
	if( creds < mincreds ) then
		local num = mincreds - creds
		ply:ChatPrint( "You have been given " .. tonumber(num) .. " credits from space welfare." )
		creds = math.max(creds, mincreds)
	end
	
	return creds
end

function GetPlayerBySteamID ( plyid )
	for _, ply in pairs (player.GetAll()) do
		if (ply:SteamID() == plyid) then
			return ply
		end
	end
	return nil
end

function SITP_UpdateCreds ( idtbl )
	if(file.Exists( SITP_FILEPATH ) == false) then return end
	local content = file.Read( SITP_FILEPATH )
	
	local contenttable = string.Explode( "\n", content )
	for k, v in pairs (contenttable) do
		local smalltbl = string.Explode( " ", v )
		for _, ply in pairs (idtbl) do
			if ( table.HasValue( smalltbl, ply ) ) then
				local plyent = GetPlayerBySteamID( ply )
				if ( plyent and plyent:IsValid()) then
					local creds = plyent:GetNetworkedInt( "sitp_creds" )
					smalltbl[2] = tostring(creds)
					local passedstr = string.Implode(" ", smalltbl)
					contenttable[k] = passedstr
				end
			end
		end
	end
	content = string.Implode("\n", contenttable)
	
	file.Write( SITP_FILEPATH, content )
end

function SITP_AddCreds ( ply, amount, distribute )
	if(!ply or !ply:IsValid()) then return end
	
	local team = ply.sitp_team
	local corpmult = 1
	
	if(team != "merc") then
		corpmult = 0.7
		
		if( distribute == true ) then
			local allplys = player.GetAll()
			for _, v in pairs (allplys) do
				if(v and v:IsValid()) then
					if (v != ply) and (v.sitp_team == ply.sitp_team) then
						SITP_AddCreds( v, amount * 0.1, false )
					end
				end
			end
		end
	end
	
	local creds = ply:GetNetworkedInt( "sitp_creds" )
	creds = math.floor(creds + amount * corpmult)
	ply:SetNetworkedNumber( "sitp_creds", creds )
end

function SITP_DeductCreds ( ply, amount, class )
	if (!ply or !ply:IsValid()) then return false end
	
	local team = ply.sitp_team
	local corpmult = 1
	
	if(team == "fsa") then
		if((class == "acf_ammo") or (class == "acf_gun")) then
			corpmult = 0.8
		end
	elseif (team == "gmc") then
		if((class == "sitp_miningtool") or (class == "sitp_core")) then
			corpmult = 0.8
		end
	end
	local creds = ply:GetNetworkedInt( "sitp_creds" )
	creds = math.floor(creds - amount * corpmult)
	if(creds < 0) then
		return false
	else
		ply:SetNetworkedNumber( "sitp_creds", creds )
		return true
	end
end

function SITP_InitialEconomyCheck ( ply )
	if ( SinglePlayer() == true ) then return end -- no economics in SP.
	
	local creds = SITP_GetCreds( ply )
	
	ply:SetNetworkedNumber("sitp_creds", creds)
	ply:ChatPrint( "You have " .. tostring(creds) .. " credits available." )
	
	local idtbl = {}
	
	for k, v in pairs (player.GetAll()) do
		idtbl[k] = v:SteamID()
	end
	
	SITP_UpdateCreds ( idtbl )
end

function SITP_EconCheckPost ( ply )
	local creds = math.max(ply:GetNetworkedInt("sitp_creds"), GetConVarNumber("sitp_defaultcredits"))
	ply:SetNetworkedNumber("sitp_creds", creds)
	
	ply:ChatPrint( "You have " .. tostring(creds) .. " credits available." )
	
	local idtbl = {}
	
	for k, v in pairs (player.GetAll()) do
		idtbl[k] = v:SteamID()
	end
	
	SITP_UpdateCreds ( idtbl )
end

hook.Add ( "PlayerInitialSpawn", "SITP Initial Economy Spawn Hook", SITP_InitialEconomyCheck )
hook.Add ( "PlayerSpawn", "SITP Economy Spawn Hook", SITP_EconCheckPost )

function SITP_EconomicTick ()
	if ( SinglePlayer() == true ) then return end -- no economics in SP.
	
	if (!SITP_NextEconTick) then
		SITP_NextEconTick = 0
	end
	
	if SITP_NextEconTick < CurTime() then
		
		local plys = player.GetAll()
		local idtbl = {}
		
		for k, v in pairs (plys) do
			idtbl[k] = v:SteamID()
			local creds = v:GetNetworkedInt("sitp_creds")
			v:ChatPrint( "Credits Synced. You have " .. tostring(creds) .. " credits." )
		end
		
		SITP_UpdateCreds ( idtbl )
	
		SITP_NextEconTick = CurTime() + 300		-- 5 minutes per update
	end
end

hook.Add ("Think", "SITP Economic Think Hook", SITP_EconomicTick)

function SITP_PlayerDies( ply, inf, atk ) 
	if( ply and ply:IsValid() and ply:IsPlayer() ) then
		if (ply.sitp_spacetype == ply.sitp_team or ply.sitp_spacetype == "all") then return end
		
		local deductamt = math.floor(ply:GetNetworkedInt( "sitp_creds" ) * 0.1)
		ply:ChatPrint( "Death and Dismemberment costs you " .. tostring(deductamt) .. " credits." )
		
		if( atk and atk:IsValid() and atk:IsPlayer() and atk != ply) then
			if( atk.sitp_team != ply.sitp_team or atk.sitp_team == "merc" ) then
				local atkcred = atk:GetNetworkedInt( "sitp_creds" )
				local bountymul = 1
				if(atk.sitp_team != "merc" and ply.sitp_team != "merc") then
					bountymul = 2
				end
				local bountyamt = ply:GetNetworkedInt( "sitp_creds" ) * 0.05 * bountymul
				SITP_AddCreds ( atk, bountyamt, true )
				atk:ChatPrint ( "For killing such a fearsome foe, you earned " .. tostring(bountyamt) .. " credits!" )
			end
		end
		local suckcreds = math.floor(math.max(ply:GetNetworkedInt( "sitp_creds" ) - deductamt, 0))
		ply:SetNetworkedNumber( "sitp_creds", suckcreds )
	end
end

hook.Add ( "PlayerDeath", "SITP Economy Player Death", SITP_PlayerDies )

function SITP_Donate( plyg, plyr, amt, anon )
	if( plyg and plyg:IsValid() and plyg:IsPlayer() and plyr and plyr:IsValid() and plyr:IsPlayer() ) then
		local gcreds = plyg:GetNetworkedInt( "sitp_creds" )
		local rcreds = plyr:GetNetworkedInt( "sitp_creds" )
		local checkamt = math.floor(math.max(amt, 0))
		if( (gcreds - checkamt) < 0 ) then
			plyg:ChatPrint ( "You do not have " .. tostring(checkamt) .. " credits to give!" )
		else
			if( checkamt == 0 ) then return end
			if (!anon or anon == false) then
				plyr:ChatPrint( plyg:Nick() .. " gave you " .. tostring(checkamt) .. " credits." )
			else
				plyr:ChatPrint( "Someone gave you " .. tostring(checkamt) .. " credits." )
			end
			plyg:ChatPrint( "You donated " .. tostring(checkamt) .. " credits to " .. plyr:Nick() .. "." )
			rcreds = rcreds + checkamt
			gcreds = gcreds - checkamt
			plyg:SetNetworkedNumber( "sitp_creds", gcreds )
			plyr:SetNetworkedNumber( "sitp_creds", rcreds )
		end
	end
end

